import { fileURLToPath, URL } from 'node:url'
import { resolve } from 'path'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
import { NaiveUiResolver } from 'unplugin-vue-components/resolvers'
import Components from 'unplugin-vue-components/vite'
import dts from 'vite-plugin-dts'  // 新增

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueDevTools(),
    Components({
      resolvers: [NaiveUiResolver()],
      dirs: ["src/components"],
      // 配置type文件生成位置
      dts: "components.d.ts",
    }),
    dts({
      rollupTypes: true,  // 合并类型声明为单个文件
      insertTypesEntry: true,  // 在package.json中插入types字段
      tsconfigPath: resolve(__dirname, "tsconfig.app.json"), // 指定tsconfig路径
    })  // 新增
  ],
  build: {
    lib: {
      entry: resolve(__dirname, 'src/index.ts'),
      name: 'vemix-ui',
      fileName: (format) => `index.${format}.js`
    },
    rollupOptions: {
      // 确保外部化处理那些你不想打包进库的依赖
      external: ['vue','naive-ui'],
      output: {
        globals: {
          vue: 'Vue',
          'naive-ui': 'naiveUI'
        }
      }
    }
  },
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    },
  },
})